package com.sita.linboard.DriverVehicle;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sita.linboard.R;
import com.sita.linboard.base.BaseApplication;
import com.sita.linboard.driverInfo.ClearBackBean;
import com.sita.linboard.http.RestClient;
import com.sita.linboard.utils.SpUtils;
import com.sita.linboard.utils.ToastUtils;

import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by Jason on 2016/9/20.
 */
public class VehicleAdapter extends BaseAdapter {
    private Context context;
    private VehicleBackBean backBean;
    private UnBindVehicleListener listener;


    public VehicleAdapter(Context context, UnBindVehicleListener unBindVehicleListener) {
        this.context = context;
        backBean = new VehicleBackBean();
        listener = unBindVehicleListener;
    }

    public void setVehicleData(VehicleBackBean data) {
        backBean = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (backBean.getData() != null) {
            return backBean.getData().size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        return backBean.getData().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        MyViewHolder holder = null;
        if (convertView == null) {
            holder = new MyViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_vehicle, parent, false);
            holder.chassis = (TextView) convertView.findViewById(R.id.chassisnumber);
            holder.license = (TextView) convertView.findViewById(R.id.licenseplace);
            holder.delete = (RelativeLayout) convertView.findViewById(R.id.vehicle_delete);
            holder.newVehicle = (RadioButton) convertView.findViewById(R.id.vehiclestate);
            convertView.setTag(holder);
        } else {
            holder = (MyViewHolder) convertView.getTag();
        }
        holder.chassis.setText("车架号: " + backBean.getData().get(position).getSn());
        holder.license.setText("车牌: " + backBean.getData().get(position).getPlateNumber());
        if (backBean.getData().get(position).getIsSelect() == 1) {
            holder.newVehicle.setChecked(true);
        } else {
            holder.newVehicle.setChecked(false);
        }
        holder.newVehicle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SelectVehicle(position);

                notifyDataSetChanged();
            }
        });

        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                unBindVehicle(position);
                backBean.getData().remove(position);
                notifyDataSetChanged();
            }
        });
        return convertView;
    }


    class MyViewHolder{
        TextView chassis,license;
        RelativeLayout delete;
        RadioButton newVehicle;

    }
    private void unBindVehicle(int position){
        UnBindVehicleRequest request = new UnBindVehicleRequest();
        request.user_id =SpUtils.getString("AccoundID",null, BaseApplication.getContext());
        request.sn_id = backBean.getData().get(position).getSn();
        request.sn_cpy = backBean.getData().get(position).getSnCpy();
        RestClient.getRestService().UnBindVehicle(request, new Callback<ClearBackBean>() {
            @Override
            public void success(ClearBackBean clearBackBean, Response response) {
                if (clearBackBean.getErrorCode().equals("0")){
                    ToastUtils.show("删除解绑成功",2);
                    listener.setChange();
                }
            }
            @Override
            public void failure(RetrofitError error) {
                ToastUtils.show("删除解绑失败",2);
            }
        });
    }

    private void  SelectVehicle(final int position){
      SelectVehicleRequest request = new SelectVehicleRequest();
        request.user_id = SpUtils.getString("AccoundID",null, BaseApplication.getContext());
        request.sn_id =  backBean.getData().get(position).getSn();
        RestClient.getRestService().chooseVehicle(request, new Callback<ClearBackBean>() {
            @Override
            public void success(ClearBackBean clearBackBean, Response response) {
                if (clearBackBean.getErrorCode().equals("0")){
                    ToastUtils.show("绑定成功",3);
                    listener.setChange();
                }
            }
            @Override
            public void failure(RetrofitError error) {
                ToastUtils.show("绑定成功",3);
            }
        });
    }
}
